{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Minimum Rounds to Complete All Tasks"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #greedy #array #hash-table #counting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #贪心 #数组 #哈希表 #计数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: minimumRounds"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #完成所有任务需要的最少轮数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个下标从 <strong>0</strong> 开始的整数数组 <code>tasks</code> ，其中 <code>tasks[i]</code> 表示任务的难度级别。在每一轮中，你可以完成 2 个或者 3 个 <strong>相同难度级别</strong> 的任务。</p>\n",
    "\n",
    "<p>返回完成所有任务需要的 <strong>最少</strong> 轮数，如果无法完成所有任务，返回<em> </em><code>-1</code><em> </em>。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>tasks = [2,2,3,3,2,4,4,4,4,4]\n",
    "<strong>输出：</strong>4\n",
    "<strong>解释：</strong>要想完成所有任务，一个可能的计划是：\n",
    "- 第一轮，完成难度级别为 2 的 3 个任务。 \n",
    "- 第二轮，完成难度级别为 3 的 2 个任务。 \n",
    "- 第三轮，完成难度级别为 4 的 3 个任务。 \n",
    "- 第四轮，完成难度级别为 4 的 2 个任务。 \n",
    "可以证明，无法在少于 4 轮的情况下完成所有任务，所以答案为 4 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>tasks = [2,3,3]\n",
    "<strong>输出：</strong>-1\n",
    "<strong>解释：</strong>难度级别为 2 的任务只有 1 个，但每一轮执行中，只能选择完成 2 个或者 3 个相同难度级别的任务。因此，无法完成所有任务，答案为 -1 。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= tasks.length &lt;= 10<sup>5</sup></code></li>\n",
    "\t<li><code>1 &lt;= tasks[i] &lt;= 10<sup>9</sup></code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [minimum-rounds-to-complete-all-tasks](https://leetcode.cn/problems/minimum-rounds-to-complete-all-tasks/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [minimum-rounds-to-complete-all-tasks](https://leetcode.cn/problems/minimum-rounds-to-complete-all-tasks/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[2,2,3,3,2,4,4,4,4,4]', '[2,3,3]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumRounds(self, tasks: List[int]) -> int:\n",
    "        retVal = 0\n",
    "\n",
    "        tasks.sort()\n",
    "\n",
    "        tasksSize = len(tasks)\n",
    "        count = 1\n",
    "        for i in range(1, tasksSize):\n",
    "            if tasks[i] == tasks[i-1]:\n",
    "                count += 1\n",
    "                continue\n",
    "\n",
    "            if count == 1:\n",
    "                retVal = -1\n",
    "                return retVal\n",
    "            retVal += (count // 3)\n",
    "            retVal += 0 if count % 3 == 0 else 1\n",
    "\n",
    "            count = 1\n",
    "\n",
    "        if count == 1:\n",
    "            retVal = -1\n",
    "            return retVal\n",
    "        retVal += (count // 3)\n",
    "        retVal += 0 if count % 3 == 0 else 1\n",
    "\n",
    "        return retVal"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumRounds(self, tasks: List[int]) -> int:\n",
    "        cnt = Counter(tasks)\n",
    "        ans = 0\n",
    "        for v in cnt.values():\n",
    "            if v == 1:\n",
    "                return -1\n",
    "            ans += (v + 2) // 3\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import defaultdict\n",
    "class Solution:\n",
    "    def minimumRounds(self, tasks: List[int]) -> int:\n",
    "        task_dict = defaultdict(int)\n",
    "        res = 0\n",
    "        for i, x in enumerate(tasks):\n",
    "            task_dict[x] += 1\n",
    "        # print(task_dict)\n",
    "        # print(task_dict.values())\n",
    "        for i, ele in enumerate(task_dict.items()):\n",
    "            if ele[1] < 2:\n",
    "                return -1\n",
    "            if ele[1] % 3 == 0:\n",
    "                res += ele[1] // 3\n",
    "            else:\n",
    "                res += ele[1] // 3 + 1\n",
    "        return res\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumRounds(self, tasks: List[int]) -> int:\n",
    "        c = Counter(tasks)\n",
    "        ret = 0\n",
    "        for k, v in c.items():\n",
    "            i = v // 3\n",
    "            j = v % 3\n",
    "            if 1 == j:\n",
    "                if 0 == i:\n",
    "                    return -1\n",
    "                ret = ret + i + 1\n",
    "            elif 2 == j:\n",
    "                ret = ret + i + 1\n",
    "            else:\n",
    "                ret = ret + i\n",
    "            #print(f\"finish {k} {v} ret {ret}\")\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumRounds(self, tasks: List[int]) -> int:\n",
    "        mapping = {}\n",
    "        for task in tasks:\n",
    "            if(task not in mapping):\n",
    "                mapping[task] = 1\n",
    "            else:\n",
    "                mapping[task] += 1\n",
    "        result = 0\n",
    "        for _, cnt in mapping.items():\n",
    "            if(cnt == 1):\n",
    "                return -1\n",
    "            if(cnt % 3 == 0):\n",
    "                result += cnt // 3\n",
    "            else:\n",
    "                result += cnt // 3 + 1\n",
    "        return result\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumRounds(self, tasks: List[int]) -> int:\n",
    "\n",
    "        cnt=Counter(tasks)\n",
    "        res=0\n",
    "        for v in cnt.values():\n",
    "            if v==1:return -1\n",
    "            res+=(v+2)//3\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumRounds(self, tasks: List[int]) -> int:\n",
    "        c = Counter(tasks)\n",
    "        ans = 0\n",
    "        for x in c.values():\n",
    "            if x == 1:\n",
    "                return -1\n",
    "            ans += x // 3 + int(x % 3 > 0)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumRounds(self, tasks: List[int]) -> int:\n",
    "        c1 = Counter(tasks)\n",
    "        res = 0\n",
    "        for value in c1.values():\n",
    "            if value == 1:\n",
    "                return -1\n",
    "            res += (value+2) // 3\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import defaultdict\n",
    "class Solution:\n",
    "    def minimumRounds(self, tasks: List[int]) -> int:\n",
    "        task_dict = defaultdict(int)\n",
    "        res = 0\n",
    "        for i, x in enumerate(tasks):\n",
    "            task_dict[x] += 1\n",
    "        # print(task_dict)\n",
    "        # print(task_dict.values())\n",
    "        for i, ele in enumerate(task_dict.values()):\n",
    "            if ele < 2:\n",
    "                return -1\n",
    "            if ele % 3 == 0:\n",
    "                res += ele // 3\n",
    "            else:\n",
    "                res += ele // 3 + 1\n",
    "        return res\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumRounds(self, tasks: List[int]) -> int:\n",
    "        c = Counter(tasks)\n",
    "        res = 0\n",
    "        for i in c:\n",
    "            steps = c[i] // 3\n",
    "            c[i] = c[i] % 3\n",
    "            if c[i] == 2:\n",
    "                steps += 1\n",
    "            if c[i] == 1:\n",
    "                if steps > 0:\n",
    "                    if (c[i]+3) == 4:\n",
    "                        steps += 1\n",
    "                else:\n",
    "                    return -1\n",
    "            res += steps\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumRounds(self, tasks: List[int]) -> int:\n",
    "        cnt=Counter(tasks)\n",
    "        res=0\n",
    "        for val in cnt.values():\n",
    "            x=val%3\n",
    "            y=val//3\n",
    "            if x==0:\n",
    "                res+=y\n",
    "            elif x==1:\n",
    "                if y==0:\n",
    "                    return -1\n",
    "                else:\n",
    "                    res+=y+1\n",
    "            else:\n",
    "                res+=y+1\n",
    "\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumRounds(self, tasks: List[int]) -> int:\n",
    "        c = Counter()\n",
    "        for t in tasks:\n",
    "            c[t] += 1\n",
    "        ans = 0\n",
    "        for v in c.values():\n",
    "            if v == 1:\n",
    "                return -1\n",
    "            if v % 3 == 1:\n",
    "                ans += (v - 3) // 3 + 2\n",
    "            elif v % 3 == 2:\n",
    "                ans += (v + 1) // 3\n",
    "            else:\n",
    "                ans += v // 3\n",
    "        return ans\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumRounds(self, tasks: List[int]) -> int:\n",
    "        c = Counter(tasks)\n",
    "        res = 0\n",
    "        for i in c:\n",
    "            if c[i] == 1:\n",
    "                return -1\n",
    "            steps = c[i] // 3\n",
    "            c[i] = c[i] % 3\n",
    "            if c[i] == 2:\n",
    "                steps += 1\n",
    "            if c[i] == 1:\n",
    "                steps += 1\n",
    "            res += steps\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumRounds(self, tasks: List[int]) -> int:\n",
    "        taskdict = {}\n",
    "        for i in tasks:\n",
    "            taskdict[i] = taskdict.get(i, 0) + 1\n",
    "        end = 0\n",
    "        for i in taskdict.values():\n",
    "            if i == 1:\n",
    "                return -1\n",
    "            elif i == 2 or i == 3:\n",
    "                end += 1\n",
    "            else:\n",
    "                if i % 2 == 1:\n",
    "                    end += 1\n",
    "                    i -= 3\n",
    "                    m, n = divmod(i, 6)\n",
    "                    end += (2 * m + n // 2)\n",
    "                elif i % 2 == 0:\n",
    "                    m, n = divmod(i, 6)\n",
    "                    end += (2 * m + n // 2)\n",
    "        return end"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumRounds(self, tasks: List[int]) -> int:\n",
    "        ans = 0\n",
    "        count_dict = defaultdict(int)\n",
    "\n",
    "        for item in tasks:\n",
    "            count_dict[item] += 1\n",
    "\n",
    "        for k, v in count_dict.items():\n",
    "            if v == 1:\n",
    "                return -1\n",
    "            else:\n",
    "                ans += int(v / 3)\n",
    "                if v % 3 != 0:\n",
    "                    ans += 1\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumRounds(self, tasks: List[int]) -> int:\n",
    "        m = Counter(tasks)\n",
    "        if 1 in m.values():\n",
    "            return -1\n",
    "        return sum((c+2)//3 for c in m.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumRounds(self, tasks: List[int]) -> int:\n",
    "        cnt = Counter(tasks)\n",
    "        rtn = 0\n",
    "        for v in cnt.values():\n",
    "            if v == 1:\n",
    "                return -1\n",
    "            rtn += (v // 6) * 2\n",
    "            v = v % 6\n",
    "            if v > 3:\n",
    "                rtn += 2\n",
    "            elif v > 0:\n",
    "                rtn += 1\n",
    "        return rtn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumRounds(self, tasks: List[int]) -> int:\n",
    "        tasks = Counter(tasks)\n",
    "        ret = 0\n",
    "        for v in tasks.values():\n",
    "            if v == 1:\n",
    "                return -1\n",
    "            ret += (v + 2) // 3\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumRounds(self, tasks: List[int]) -> int:\n",
    "        task_counter = collections.Counter(tasks)\n",
    "        ans = 0\n",
    "        for tc in task_counter.values():\n",
    "            if tc == 1:\n",
    "                return -1\n",
    "            ans += math.ceil(tc / 3)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumRounds(self, tasks: List[int]) -> int:\n",
    "        cnt = Counter(tasks)\n",
    "        ans = 0\n",
    "        for c in cnt.values():\n",
    "            if c == 1:\n",
    "                return -1\n",
    "            ans += (c+2)//3\n",
    "        return ans "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumRounds(self, tasks: List[int]) -> int:\n",
    "        res=Counter(tasks)\n",
    "        ans=0\n",
    "        for tt in res.values():\n",
    "            if tt==1:\n",
    "                return -1\n",
    "            ans+=(tt+2)//3\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumRounds(self, tasks: List[int]) -> int:\n",
    "        cnt = Counter(tasks)\n",
    "        return sum((c + 2) // 3 for c in cnt.values()) if 1 not in cnt.values() else -1\n",
    "\n",
    "\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumRounds(self, tasks: List[int]) -> int:\n",
    "        freq = Counter(tasks)\n",
    "        ans = 0\n",
    "        for (a, b) in freq.items():\n",
    "            if b == 1: return -1\n",
    "            else: ans += (b + 2) // 3\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumRounds(self, tasks: List[int]) -> int:\n",
    "        cnt=Counter(tasks)\n",
    "        ##2，3：1\n",
    "        ##4，5，6：2\n",
    "        ans=0\n",
    "        for k in cnt:\n",
    "            if cnt[k]==1:return -1\n",
    "            ans+=(cnt[k]+2)//3\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumRounds(self, tasks: List[int]) -> int:\n",
    "        cnt = Counter(tasks)\n",
    "        ans = 0\n",
    "        for v in cnt.values():\n",
    "            if v == 1:\n",
    "                return -1\n",
    "            if v % 3 == 0:\n",
    "                ans += v // 3\n",
    "            else:\n",
    "                ans += v // 3 + 1\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumRounds(self, tasks: List[int]) -> int:\n",
    "        def h(n):\n",
    "            if n==1: return inf\n",
    "            a, b = divmod(n, 3)\n",
    "            if b==0:\n",
    "                return a\n",
    "            if b==2:\n",
    "                return a+1\n",
    "            if b==1:\n",
    "                return a+1\n",
    "        d = defaultdict(int)\n",
    "        for ai in tasks:\n",
    "            d[ai] += 1\n",
    "        res = 0\n",
    "        for k, cnt in d.items():\n",
    "            res += h(cnt)\n",
    "            if res==inf:\n",
    "                return -1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumRounds(self, tasks: List[int]) -> int:\n",
    "        tasks_counts = Counter(tasks)\n",
    "\n",
    "        rounds = 0\n",
    "        for count in tasks_counts.values():\n",
    "            if count == 1:\n",
    "                return -1\n",
    "\n",
    "            rounds += math.ceil(count / 3)\n",
    "            \n",
    "\n",
    "        return int(rounds)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumRounds(self, tasks: List[int]) -> int:\n",
    "        \"\"\"\n",
    "        在排除num == 1的情况下，无法用 2 * x + 3* y == 1; x,y \\in N*\n",
    "        如果num % 3 = 1\n",
    "            则将三组中的取出一个，使用2 + 2 代替 3 + 1\n",
    "        如果 num % 3 = 2\n",
    "            则再加一个2进行运行\n",
    "        \"\"\"\n",
    "        task_counter = collections.Counter(tasks)\n",
    "        ans = 0\n",
    "        for tc in task_counter.values():\n",
    "            if tc == 1:\n",
    "                return -1\n",
    "            ans += math.ceil(tc / 3)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumRounds(self, tasks: List[int]) -> int:\n",
    "        cnt=Counter(tasks)\n",
    "        def f(x):\n",
    "            if x%3==0:\n",
    "                return x//3\n",
    "            elif x%3==1:\n",
    "                return x//3+1\n",
    "            else:\n",
    "                return x//3+1\n",
    "        if any(cnt[x]==1 for x in cnt):\n",
    "            return -1\n",
    "        res=0\n",
    "        for x in cnt:\n",
    "            res+=f(cnt[x])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumRounds(self, tasks: List[int]) -> int:\n",
    "        cnt = Counter(tasks)\n",
    "        ans = 0\n",
    "        for v in cnt.values():\n",
    "            if v == 1:\n",
    "                return -1\n",
    "            rest = v % 3\n",
    "            if rest == 0:\n",
    "                ans += v // 3\n",
    "            elif rest == 1:\n",
    "                ans += (v - 4) // 3 + 2\n",
    "            else:\n",
    "                ans += (v - 2) // 3 + 1\n",
    "        return ans "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def Recursion(self, num, processTime):\n",
    "        if num == 1:\n",
    "            return -1\n",
    "        \n",
    "        if num % 3 != 0:\n",
    "            return num//3 + 1\n",
    "\n",
    "        return num//3\n",
    "    def minimumRounds(self, tasks: List[int]) -> int:\n",
    "        processDict = {}\n",
    "        for i in tasks:\n",
    "            if i not in processDict:\n",
    "                processDict[i] = 1\n",
    "                continue\n",
    "\n",
    "            processDict[i] += 1\n",
    "\n",
    "        processList = processDict.values()\n",
    "\n",
    "        result = 0\n",
    "        for i in processList:\n",
    "            processTime = self.Recursion(i, 0)\n",
    "            if processTime == -1:\n",
    "                return -1\n",
    "            result += processTime\n",
    "\n",
    "        return result\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import defaultdict\n",
    "class Solution:\n",
    "    def minimumRounds(self, tasks: List[int]) -> int:\n",
    "        task_dict = defaultdict(int)\n",
    "        res = 0\n",
    "        for i, x in enumerate(tasks):\n",
    "            task_dict[x] += 1\n",
    "        # print(task_dict)\n",
    "        # print(task_dict.values())\n",
    "        for i, ele in enumerate(task_dict.values()):\n",
    "            if ele < 2:\n",
    "                return -1\n",
    "            if ele % 3 == 0:\n",
    "                res += ele // 3\n",
    "            else:\n",
    "                res += ele // 3 + 1\n",
    "        return res\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumRounds(self, tasks: List[int]) -> int:\n",
    "        d=Counter(tasks)\n",
    "        num=list(d.values())\n",
    "        if min(num)<2:\n",
    "            return -1\n",
    "        print(num)\n",
    "        count=0\n",
    "        for i in num:\n",
    "            while i-3>=2:\n",
    "                i-=3\n",
    "                count+=1\n",
    "            if i==2:\n",
    "                count+=1\n",
    "            elif i==4:\n",
    "                count+=2\n",
    "            elif i==3:\n",
    "                count+=1\n",
    "        return count\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumRounds(self, tasks: List[int]) -> int:\n",
    "        mapping = {}\n",
    "        for task in tasks:\n",
    "            if(task not in mapping):\n",
    "                mapping[task] = 1\n",
    "            else:\n",
    "                mapping[task] += 1\n",
    "        result = 0\n",
    "        for _, cnt in mapping.items():\n",
    "            if(cnt == 1):\n",
    "                return -1\n",
    "            if(cnt % 3 == 0):\n",
    "                result += cnt // 3\n",
    "            else:\n",
    "                result += cnt // 3 + 1\n",
    "        return result\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumRounds(self, tasks: List[int]) -> int:\n",
    "        cnt = Counter(tasks)\n",
    "        if 1 in cnt.values():\n",
    "            return - 1\n",
    "        return sum((v + 2)// 3 for v in cnt.values())\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumRounds(self, tasks: List[int]) -> int:\n",
    "        cnt=Counter(tasks)\n",
    "        ##2，3：1\n",
    "        ##4，5，6：2\n",
    "        ans=0\n",
    "        for v in cnt.values():\n",
    "            if v==1:return -1\n",
    "            ans+=(v+2)//3\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumRounds(self, tasks: List[int]) -> int:\n",
    "        cnt = Counter(tasks)\n",
    "        ans = 0\n",
    "        if 1 not in cnt.values(): \n",
    "            for c in cnt.values():\n",
    "                ans += (c + 2) // 3\n",
    "            return ans \n",
    "        else:\n",
    "            return -1\n",
    "\n",
    " "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumRounds(self, tasks: List[int]) -> int:\n",
    "        c = Counter(tasks)\n",
    "        ans = 0\n",
    "        for k, v in c.items():\n",
    "            if v == 1:\n",
    "                return -1\n",
    "            ans += v // 3\n",
    "            if v % 3:\n",
    "                ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumRounds(self, tasks: List[int]) -> int:\n",
    "        字典 = dict()\n",
    "        for i in tasks:\n",
    "            字典[i] = 字典.get(i, 0) + 1\n",
    "        最少轮数 = 0\n",
    "        for i in 字典.values():\n",
    "            if i == 1:\n",
    "                return -1\n",
    "            最少轮数 += (i+2)//3\n",
    "        return 最少轮数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumRounds(self, tasks: List[int]) -> int:\n",
    "        c = Counter(tasks)\n",
    "        ans = 0\n",
    "        for v in c:\n",
    "            cur = c[v]\n",
    "            if cur == 1:\n",
    "                return -1\n",
    "            if cur % 3 == 0:\n",
    "                ans += cur // 3\n",
    "            elif cur % 3 == 2:\n",
    "                ans += cur // 3 + 1\n",
    "            elif cur % 3 == 1:\n",
    "                ans += cur // 3 - 1\n",
    "                left = cur - 3 * (cur // 3 - 1)\n",
    "                if left & 1:\n",
    "                    return -1\n",
    "                ans += left // 2\n",
    "        return ans\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "class Solution:\n",
    "    def minimumRounds(self, tasks: List[int]) -> int:\n",
    "        res = 0\n",
    "        counter = Counter(tasks)\n",
    "        for k, cnt in counter.items():\n",
    "            if cnt == 1:\n",
    "                return -1\n",
    "            if cnt % 3 == 0:\n",
    "                res += cnt // 3\n",
    "            else:\n",
    "                res += cnt // 3 + 1\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumRounds(self, tasks: List[int]) -> int:\n",
    "        cnt = Counter(tasks)\n",
    "        if 1 in cnt.values(): return -1\n",
    "        return sum(map(lambda x:(x+2)//3,cnt.values()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumRounds(self, tasks: List[int]) -> int:\n",
    "        cnts = collections.Counter(tasks)\n",
    "\n",
    "        res = 0\n",
    "        for key, val in cnts.items():\n",
    "            if val % 3 == 0:\n",
    "                res += val // 3\n",
    "            elif val % 3 == 2:\n",
    "                res += val // 3 + 1\n",
    "            else:\n",
    "                if val == 1:\n",
    "                    return -1\n",
    "                else:\n",
    "                    res += (val - 4) // 3 + 2\n",
    "        \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import defaultdict\n",
    "class Solution:\n",
    "    def minimumRounds(self, tasks: List[int]) -> int:\n",
    "        task_dict = defaultdict(int)\n",
    "        res = 0\n",
    "        for i, x in enumerate(tasks):\n",
    "            task_dict[x] += 1\n",
    "        # print(task_dict)\n",
    "        # print(task_dict.values())\n",
    "        for i, ele in enumerate(task_dict.items()):\n",
    "            if ele[1] < 2:\n",
    "                return -1\n",
    "            if ele[1] % 3 == 0:\n",
    "                res += ele[1] // 3\n",
    "            else:\n",
    "                res += ele[1] // 3 + 1\n",
    "        return res\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import defaultdict\n",
    "class Solution:\n",
    "    def minimumRounds(self, tasks: List[int]) -> int:\n",
    "        task_dict = defaultdict(int)\n",
    "        res = 0\n",
    "        for i, x in enumerate(tasks):\n",
    "            task_dict[x] += 1\n",
    "        # print(task_dict)\n",
    "        # print(task_dict.values())\n",
    "        for _, ele in task_dict.items():\n",
    "            if ele < 2:\n",
    "                return -1\n",
    "            if ele % 3 == 0:\n",
    "                res += ele // 3\n",
    "            else:\n",
    "                res += ele // 3 + 1\n",
    "        return res\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumRounds(self, tasks: List[int]) -> int:\n",
    "        cnt = Counter(tasks)\n",
    "        ans = 0\n",
    "        for _, num in cnt.items():\n",
    "            if num % 3 == 0:\n",
    "                ans += num // 3\n",
    "            elif num % 3 == 1:\n",
    "                if num == 1:\n",
    "                    return -1\n",
    "                ans += (num - 4) // 3 + 2\n",
    "            else:\n",
    "                ans += (num - 2) // 3 + 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumRounds(self, tasks: List[int]) -> int:\n",
    "        cnt = Counter(tasks)\n",
    "        return sum((c + 2) // 3 for c in cnt.values()) if 1 not in cnt.values() else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumRounds(self, food: List[int]) -> int:\n",
    "        cnt = Counter(food)\n",
    "        return sum((c + 2) // 3 for c in cnt.values()) if 1 not in cnt.values() else -1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumRounds(self, tasks: List[int]) -> int:\n",
    "        Conter = collections.Counter(tasks)\n",
    "        heap = []\n",
    "        count = 0\n",
    "        for key, value in Conter.items():\n",
    "            if value == 1:return -1\n",
    "            else:\n",
    "                count += value//3\n",
    "                if value%3:\n",
    "                    count += 1\n",
    "        return count\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumRounds(self, tasks: List[int]) -> int:\n",
    "\n",
    "\n",
    "\n",
    "        cnt = collections.Counter(tasks)\n",
    "        \n",
    "\n",
    "\n",
    "        sort_task = sorted(cnt.values(), reverse=True)\n",
    "\n",
    "        round_ = 0\n",
    "\n",
    "        while sort_task:\n",
    "            if sort_task[-1] == 1:\n",
    "                return -1\n",
    "            if sort_task[-1] % 2 == 1 or sort_task[-1] % 3 == 0:\n",
    "                sort_task[-1] -= 3\n",
    "            \n",
    "            else:\n",
    "                sort_task[-1] -= 2\n",
    "\n",
    "            if sort_task[-1] == 0:\n",
    "                sort_task.pop()\n",
    "\n",
    "            round_ += 1\n",
    "        return round_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumRounds(self, tasks: List[int]) -> int:\n",
    "      tasks = collections.Counter(tasks)\n",
    "      cnt = 0\n",
    "      for val in tasks.values():\n",
    "        if val == 1:\n",
    "          return -1\n",
    "        if val % 3:\n",
    "          cnt += val // 3 + 1\n",
    "        else:\n",
    "          cnt += val // 3\n",
    "      return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumRounds(self, tasks: List[int]) -> int:\n",
    "        tasks.sort()\n",
    "        res = 0\n",
    "        \n",
    "        cnt = defaultdict(int)\n",
    "        for i in tasks:\n",
    "            cnt[i]+=1\n",
    "\n",
    "        for i in cnt.values():\n",
    "        \n",
    "            if i==1:\n",
    "                return -1\n",
    "            if i%3:\n",
    "                res+=1\n",
    "            res+=i//3\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumRounds(self, tasks: List[int]) -> int:\n",
    "        字典 = dict()\n",
    "        for i in tasks:\n",
    "            字典[i] = 字典.get(i, 0) + 1\n",
    "        最少轮数 = 0\n",
    "        for i in 字典.values():\n",
    "            if i == 1:\n",
    "                return -1\n",
    "            最少轮数 += i//3 + (i%3 > 0)\n",
    "        return 最少轮数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumRounds(self, tasks: List[int]) -> int:\n",
    "        cnt = Counter(tasks)\n",
    "        ans = 0\n",
    "        for _, num in cnt.items():\n",
    "            if num % 3 == 0:\n",
    "                ans += num // 3\n",
    "            elif num % 3 == 1:\n",
    "                if num == 1:\n",
    "                    return -1\n",
    "                ans += (num - 4) // 3 + 2\n",
    "            else:\n",
    "                ans += (num - 2) // 3 + 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumRounds(self, tasks: List[int]) -> int:\n",
    "        counter = Counter(tasks)\n",
    "        res = 0\n",
    "        for cnt in counter.values():\n",
    "            if cnt==1:\n",
    "                return -1\n",
    "            if cnt%3==1 or cnt%3==2:\n",
    "                res+=cnt//3+1\n",
    "            else:\n",
    "                res+=cnt//3\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import math\n",
    "class Solution:\n",
    "    def minimumRounds(self, tasks: List[int]) -> int:\n",
    "        ans = 0\n",
    "        Hashmap = {}\n",
    "        for i in tasks:\n",
    "            if str(i) not in Hashmap:\n",
    "                Hashmap[str(i)] = 0\n",
    "            Hashmap[str(i)] += 1\n",
    "        for value in Hashmap.values():\n",
    "            if value < 2: return -1\n",
    "            ans += math.ceil(value/3)\n",
    "        \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from queue import Queue\n",
    "\n",
    "class Solution:\n",
    "    def minimumRounds(self, tasks: List[int]) -> int:\n",
    "        tasks.sort()\n",
    "        num_map = {}\n",
    "        max_count = 0\n",
    "        for i in range(len(tasks)):\n",
    "            if tasks[i] in num_map:\n",
    "                num_map[tasks[i]] = num_map[tasks[i]] + 1\n",
    "                max_count = max(num_map[tasks[i]], max_count)\n",
    "            else:\n",
    "                num_map[tasks[i]] = 1\n",
    "                max_count = max(1, max_count)\n",
    "\n",
    "        tmp_array = [-1] * max((max_count+1), 4)\n",
    "        tmp_array[2] = 1\n",
    "        tmp_array[3] = 1\n",
    "\n",
    "        tmp_list = Queue()\n",
    "        tmp_list.put(2)\n",
    "        tmp_list.put(3)\n",
    "        count = 1\n",
    "        while tmp_list.qsize() > 0:\n",
    "            n = tmp_list.qsize()\n",
    "            for i in range(n):\n",
    "                t1 = tmp_list.get()\n",
    "                t2 = t1 + 2\n",
    "                t3 = t1 + 3\n",
    "                if t2 <= max_count and tmp_array[t2] <= 0:\n",
    "                    tmp_array[t2] = count + 1\n",
    "                    tmp_list.put(t2)\n",
    "                if t3 <= max_count and tmp_array[t3] <= 0:\n",
    "                    tmp_array[t3] = count + 1\n",
    "                    tmp_list.put(t3)\n",
    "\n",
    "            count += 1\n",
    "\n",
    "        res = 0\n",
    "        for key, value in num_map.items():\n",
    "            if tmp_array[value] <= 0:\n",
    "                return -1\n",
    "            else:\n",
    "                res += tmp_array[value]\n",
    "\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from queue import Queue\n",
    "\n",
    "class Solution:\n",
    "    def minimumRounds(self, tasks: List[int]) -> int:\n",
    "        tasks.sort()\n",
    "        num_map = {}\n",
    "        max_count = 0\n",
    "        for i in range(len(tasks)):\n",
    "            if tasks[i] in num_map:\n",
    "                num_map[tasks[i]] = num_map[tasks[i]] + 1\n",
    "                max_count = max(num_map[tasks[i]], max_count)\n",
    "            else:\n",
    "                num_map[tasks[i]] = 1\n",
    "                max_count = max(1, max_count)\n",
    "\n",
    "        tmp_array = [-1] * max((max_count+1), 4)\n",
    "        tmp_array[2] = 1\n",
    "        tmp_array[3] = 1\n",
    "\n",
    "        tmp_list = deque([2, 3])\n",
    "        count = 1\n",
    "        while len(tmp_list) > 0:\n",
    "            n = len(tmp_list)\n",
    "            for i in range(n):\n",
    "                t1 = tmp_list.popleft()\n",
    "                t2 = t1 + 2\n",
    "                t3 = t1 + 3\n",
    "                if t2 <= max_count and tmp_array[t2] <= 0:\n",
    "                    tmp_array[t2] = count + 1\n",
    "                    tmp_list.append(t2)\n",
    "                if t3 <= max_count and tmp_array[t3] <= 0:\n",
    "                    tmp_array[t3] = count + 1\n",
    "                    tmp_list.append(t3)\n",
    "\n",
    "            count += 1\n",
    "\n",
    "        res = 0\n",
    "        for key, value in num_map.items():\n",
    "            if tmp_array[value] <= 0:\n",
    "                return -1\n",
    "            else:\n",
    "                res += tmp_array[value]\n",
    "\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumRounds(self, tasks: List[int]) -> int:\n",
    "        # put task data into a map\n",
    "        map = Counter(tasks)\n",
    "\n",
    "        rounds = 0\n",
    "        efficiency = [2, 3]\n",
    "\n",
    "        # perform a coin exchange dp in each entry of map, \n",
    "        for level in map.keys():\n",
    "            level_task = map.get(level)\n",
    "            dp = [level_task + 1] * (level_task + 1)\n",
    "            dp[0] = 0\n",
    "\n",
    "            for i in range(1, level_task + 1):\n",
    "                for e in efficiency:\n",
    "                    if i >= e:\n",
    "                        dp[i] = min(dp[i], dp[i - e] + 1)\n",
    "                if i == level_task:\n",
    "                    if dp[i] > level_task:\n",
    "                        return -1\n",
    "                    else:\n",
    "                        rounds += dp[i]\n",
    "        return rounds\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumRounds(self, tasks: List[int]) -> int:\n",
    "        # put task data into a map\n",
    "        map = {}\n",
    "        for task in tasks:\n",
    "            map[task] = map.get(task, 0) + 1\n",
    "\n",
    "        rounds = 0\n",
    "        efficiency = [2, 3]\n",
    "        \n",
    "        # perform a coin exchange dp in each entry of map, \n",
    "        for level in map.keys():\n",
    "            level_task = map.get(level)\n",
    "            dp = [level_task + 1] * (level_task + 1)\n",
    "            dp[0] = 0\n",
    "\n",
    "            for i in range(1, level_task + 1):\n",
    "                for e in efficiency:\n",
    "                    if i >= e:\n",
    "                        dp[i] = min(dp[i], dp[i - e] + 1)\n",
    "                if i == level_task:\n",
    "                    if dp[i] > level_task:\n",
    "                        return -1\n",
    "                    else:\n",
    "                        rounds += dp[i]\n",
    "        return rounds\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minimumRounds(self, tasks: List[int]) -> int:\n",
    "        a=Counter(tasks)\n",
    "        cnt=0\n",
    "        for num in set(tasks):\n",
    "            if a[num]==1:\n",
    "                return -1\n",
    "\n",
    "            if a[num]%3==0:\n",
    "                cnt+=a[num]//3\n",
    "            else:\n",
    "                cnt+=a[num]//3+1\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from queue import Queue\n",
    "\n",
    "class Solution:\n",
    "    def minimumRounds(self, tasks: List[int]) -> int:\n",
    "        tasks1 = Counter(tasks).most_common()\n",
    "        max_count = tasks1[0][1]\n",
    "\n",
    "        tmp_array = [-1] * max((max_count+1), 4)\n",
    "        tmp_array[2] = 1\n",
    "        tmp_array[3] = 1\n",
    "\n",
    "        tmp_list = deque([2, 3])\n",
    "        count = 1\n",
    "        while len(tmp_list) > 0:\n",
    "            n = len(tmp_list)\n",
    "            for i in range(n):\n",
    "                t1 = tmp_list.popleft()\n",
    "                t2 = t1 + 2\n",
    "                t3 = t1 + 3\n",
    "                if t2 <= max_count and tmp_array[t2] <= 0:\n",
    "                    tmp_array[t2] = count + 1\n",
    "                    tmp_list.append(t2)\n",
    "                if t3 <= max_count and tmp_array[t3] <= 0:\n",
    "                    tmp_array[t3] = count + 1\n",
    "                    tmp_list.append(t3)\n",
    "\n",
    "            count += 1\n",
    "\n",
    "        res = 0\n",
    "        for key, value in tasks1:\n",
    "            if tmp_array[value] <= 0:\n",
    "                return -1\n",
    "            else:\n",
    "                res += tmp_array[value]\n",
    "\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "class Solution:\n",
    "    def minimumRounds(self, tasks: List[int]) -> int:\n",
    "        d = dict(Counter(tasks))\n",
    "        nums = d.values()\n",
    "        if 1 in nums:\n",
    "            return -1\n",
    "        ans = 0\n",
    "        for num in nums:\n",
    "            if num%3 == 0:\n",
    "                ans += num//3\n",
    "            elif num%3 == 1:\n",
    "                ans += 2+(num-4)//3\n",
    "            elif num%3 == 2:\n",
    "                ans += 1+(num-2)//3\n",
    "        return ans\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
